package controls;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Ahm
 */
import java.sql.*;
import java.util.ArrayList;
import java.util.Random;
public class addinfo {

    public int randomimagecount() {
Random r = new Random();
int deger = r.nextInt(5)+1;
        return deger;
    }

    public int membercount() {
        int uyesayisi=0;
        try {
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
             ResultSet sonuc = komut.executeQuery("Select * From usertable");
            while(sonuc.next()){
            sonuc.getString("id");
                    uyesayisi++;
            }

        } catch (Exception e) {
        }
        return uyesayisi;
    }

    public void addcomment(String comment, int id, int owner) {
        controls.addinfo nesne = new controls.addinfo();
comment = nesne.stringduzelt(comment);
        try {
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
            komut.executeUpdate("INSERT INTO commenttable (Owner,DisID,Comment) VALUES ('" + owner + "','" + id + "','" + comment + "')");
        } catch (Exception e) {
        }
    }

    public void addarticle(int owner, String subject, String link, String Categories, String kisatanim,int permission) {
        try {
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
            komut.executeUpdate("INSERT INTO articletable (owner,Link,Subject,ShortSummary,Categories,Permission,`Check`) VALUES ('" +
                    owner + "','" + link + "','" + subject + "','" + kisatanim + "','" + Categories + "','" + permission + "',1)");
        } catch (Exception e) {
        }
    }

    public void addDiscoussion(String konuismi, String konukisatanim, String konu, String izinler, String type, int email, int owner, String Discoussion) {
        try {
           // Date date = new Date(
           // String adate = date.getHours() + date.getMinutes() + date.getDay() + date.getMonth() + date.getYear() + " ";
           // +"','"+ adate
            String adeta="tarihsorunuvarrrrrrr";
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
            int izin = 0;
            if(izinler.charAt(0)=='H'){izin=10000;}
 else if(izinler.charAt(0)=='S'){izin=1000;}
 else if(izinler.charAt(0)=='G'){izin=100;}
 else if(izinler.charAt(0)=='A'){izin=10;}
            komut.executeUpdate("INSERT INTO discussiontable (Ownerid,Permission,Email,ShortDescription,Discussion,Type,Subject,Date) VALUES ('"
                    + owner + "','" + izin + "','" + email + "','" + konukisatanim + "','" + Discoussion + "','" + type + "','" + konu +"','"+ adeta +"')");

        } catch (Exception e) {
        }
    }

    public void addmember(String kadi, String sifre, int okulnumarasi, String adsoyad, String birthday, String mobilephone,
            String email, String imza, String webpage, String msn, String skype, String other, int md5) {
        try {
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
            Statement komut2 = sqlconnect.GetUserTableConnection();
            komut.executeUpdate("INSERT INTO usertable (StudentNumber,UserName,Password,RealName,Authority,EMailConfirmation,AdminConfirmation) VALUES ('"
                    + okulnumarasi + "','" + kadi + "','" + sifre + "','" + adsoyad + "',10000,2,2)");

            komut2.executeUpdate("INSERT INTO profiltable (Birthday_Show,MobilePhone_Show,EMail_Show,Signature,WebSite,MSN_Show,Skype_Show,Other_Show,Email_Confirm_code) VALUES ('"
                    + birthday + "','" + mobilephone + "','" + email + "','" + imza + "','" + webpage + "','"
                    + msn + "','" + skype + "','" + other + "','" + md5 + "')");



        } catch (Exception e) {
        }
    }

    public String noktalivirgul(String anametin, String yan) {
        String metin = anametin;
        String metin2 = yan;
        int metin3;
        metin2 = metin2 + " ";
        if (metin2.equals("ON")) {
            metin3 = 1;
        } else {
            metin3 = 2;
        }
        metin = (metin + ";" + metin3);
        return metin;
    }

    public int randommd5() {
        Random r = new Random();
        int sayi = r.nextInt(1000) + 1000;
        return sayi;
    }

    public boolean nickkontroluygun(String nick) {
boolean kontrol = true;
 controls.SQLConnection sqlconnect = new controls.SQLConnection();
 Statement komut = sqlconnect.GetUserTableConnection();
        try {
            ResultSet rs = komut.executeQuery("SELECT * FROM UserTable Where UserName='" + nick+"'");
            while(rs.next()){
            kontrol=false;
            }
        } catch (SQLException ex) {
        }

return kontrol;
    }

    public String idsahibibul(int id) {
        String isim = " ";
        try {
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
            ResultSet sonuc = komut.executeQuery("SELECT * FROM usertable WHERE id='" + id + "'");
            while (sonuc.next()) {
                isim = sonuc.getString("RealName");
            }


        } catch (Exception e) {
        }
        return isim;
    }

    public void uyeyonetimonay(int id) {
        try {
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
                komut.executeUpdate("UPDATE usertable SET AdminConfirmation = 1 WHERE id = '" + id + "'");

        } catch (Exception e) {
        }
    }

    public void articleonay(int id) {
        try {
            controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
                komut.executeUpdate("UPDATE articletable SET `Check` = 2 WHERE id = '" + id + "'");
        } catch (Exception e) {
        }
    }

    public boolean aktivasyonkodonaylama(String kadi, int onaykodu) {
        boolean kontrol = false;
        int id = 0;
        controls.SQLConnection sqlconnect = new controls.SQLConnection();
        Statement komut = sqlconnect.GetUserTableConnection();
        try {
            ResultSet sonuc = komut.executeQuery("SELECT * FROM usertable WHERE UserName='" + kadi + "'");
            while (sonuc.next()) {
                id = sonuc.getInt("id");
            }
            ResultSet sonuc2 = komut.executeQuery("SELECT * FROM profiltable WHERE id='" + id + "'");
            while (sonuc2.next()) {
                int activationcode = sonuc2.getInt("Email_Confirm_code");
                if (activationcode == onaykodu) {
                    kontrol = true;
                }
            }
            if (kontrol == true) {
                komut.executeUpdate("UPDATE usertable SET EMailConfirmation = 1 WHERE id = '" + id + "'");
            }
        } catch (Exception e) {
        }

        return kontrol;
    }


    public String stringduzelt(String text) {
        for (int i = 0; i < text.length(); i++) {

          if (text.charAt(i) == '"') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "\"" + text3; i++; }}
            if (text.charAt(i) == '\'') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "\'" + text3; i++; }}
            if (text.charAt(i) == '<') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "<" + text3; i++; }}
            if (text.charAt(i) == '>') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + ">" + text3; i++; }}
            if (text.charAt(i) == '&') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "&" + text3; i++; }}
            if (text.charAt(i) == ';') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + ";" + text3; i++; }}

       
    }
         return text;
    }

    public long longduzelt(Long tex) {
String text = tex.toString();
        for (int i = 0; i < text.length(); i++) {


          if (text.charAt(i) == '"') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "\"" + text3; i++; }}
            if (text.charAt(i) == '\'') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "\'" + text3; i++; }}
            if (text.charAt(i) == '<') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "<" + text3; i++; }}
            if (text.charAt(i) == '>') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + ">" + text3; i++; }}
            if (text.charAt(i) == '&') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + "&" + text3; i++; }}
            if (text.charAt(i) == ';') { if (text.charAt(1 - 1) != '\\') {   String text2 = text.substring(0, i); String text3 = text.substring(i + 1, text.length());  text = text2 + "\\" + ";" + text3; i++; }}


    }
         return tex;
    }


    public ArrayList<String> memberarama(String text){
      ArrayList<String> memberlist = new ArrayList<String>();
        controls.SQLConnection sqlconnect = new controls.SQLConnection();
            Statement komut = sqlconnect.GetUserTableConnection();
             ResultSet sonuc;
        try {
            sonuc = komut.executeQuery("Select * From UserTable WHERE UserName LIKE '%" + text + "%'OR RealName LIKE'%" + text + "%'");
             while(sonuc.next()){
String id = sonuc.getString("id");
String isim = sonuc.getString("RealName");
                 memberlist.add(id);
                 memberlist.add(isim);
                  }
        } catch (SQLException ex) {
        }

        return memberlist;
    }




}
